import { createRouter, createWebHashHistory } from "vue-router"
import { getToken } from "@/library/tools.js";

// 2. 定义一些路由
// 每个路由都需要映射到一个组件。
// 我们后面再讨论嵌套路由。
const routes = [
    {
        path: '/',
        name: "layout",
        component: () => import("@/views/layout/index.vue"),
        children: [
            {
                path: '/',
                redirect: '/orders'
            },
            {
                path: "/orders",
                name: "orders",
                component: () => import("@/views/orders/index.vue")
            },
            {
                path: "/customers",
                name: "customers",
                component: () => import("@/views/customers/index.vue")
            },
            {
                path: "/products",
                name: "products",
                component: () => import("@/views/products/index.vue")
            },
            {
                path:'/vegetable',
                name:'vegetable',
                component:() => import('@/views/vegetable/Index.vue')
            },
            {
                path:'/vegetableDetails',
                name:'vegetableDetails',
                component:() => import('@/views/vegetable/components/vegetableDetails.vue')
            },
        ]
    },

    {
        path: "/login",
        name: "login",
        component: () => import("@/views/login/index.vue"),
    }
]

// 3. 创建路由实例并传递 `routes` 配置
// 你可以在这里输入更多的配置，但我们在这里
// 暂时保持简单
const router = createRouter({
    // 4. 内部提供了 history 模式的实现。为了简单起见，我们在这里使用 hash 模式。
    history: createWebHashHistory(),
    routes, // `routes: routes` 的缩写
})
router.beforeEach((to, from, next) => {
    // 检查token
    let token = getToken()

    if (token === null && to.path !== "/login") {
        next("/login")
    } else {
        next()
    }
})
export default router